# -*- coding: utf-8 -*-
import datetime
import time
import aircv as ac
import os
import random


# 对比两张图，找到坐标。
def matchImg(imgsrc, imgobj):  # imgsrc=原始图像，imgobj=待查找的图片
    imsrc = ac.imread(imgsrc)
    imobj = ac.imread(imgobj)
    match_result = ac.find_template(imsrc, imobj,
                                    0.9)
    # 0.9、confidence是精度，越小对比的精度就越低 {'confidence': 0.5435812473297119,
    # 'rectangle': ((394, 384), (394, 416), (450, 384), (450, 416)), 'result': (422.0, 400.alipay_leave0)}
    if match_result is not None:
        match_result['shape'] = (imsrc.shape[1], imsrc.shape[0])  # 0为高，1为宽
    return match_result

# 截图
def screencap():
    try:
        # 截图
        os.popen('adb -s c49089c6 shell screencap -p /storage/emulated/0/Pictures/Screenshots/phoneScreencap.png')
        time.sleep(2)
        # print("截图")
        # 发送到电脑
        os.popen('adb -s c49089c6 pull /storage/emulated/0/Pictures/Screenshots/phoneScreencap.png ')
        time.sleep(2)
    except Exception as e:
        print(e)
        print("截图异常")

#进入识别
def shibie(imgobj):
    try:
        screencap()
        # time.sleep(2)
        imgsrc = "./phoneScreencap.png"
        # imgobj = "./ttlxj/ico/ttlxj.png"
        print(matchImg(imgsrc,imgobj))
        myx = str(matchImg(imgsrc,imgobj)['result'][0])
        myy = str(matchImg(imgsrc,imgobj)['result'][1])
        print(myy,myx)
        os.popen('adb -s c49089c6 shell input tap ' + myx + ' ' + myy, 'r', 1)
    except Exception as e:
        print(e)
        print("操作异常")


path = r"./ico/"
jrqd = ['qiandao.png','tuichu.png','queding.png']#进入签到领取
jiaoshui = ['jiaoshui.png','qiuxiao.png','jiaoshui.png','qiuxiao.png','jiaoshui.png','qiuxiao_1.png']#浇水4次,共12壶水
jhf = ['xiuyaojiahf.png','qusy.png','dahf.png','syong.png']#需要加化肥
dasd = ['cdsd.png','kelq.png','qued1.png','tuihui.png']#大水滴领取
lsd1 = ['lsd.png','qiulq.png','dahf1.png','qued1.png','queding.png']#水滴领取
lsd2 = ['lsd.png','qiulq.png','qued1.png','queding.png']#水滴领取
lsd3 = ['lsd.png','qiulq.png','qued1.png','queding.png','tuihui.png']#水滴领取带红点
lsd = ['lsd.png','qiulq.png','qued1.png','queding.png','tuihui.png']#水滴领取
imgobjs = [jrqd,jiaoshui,jhf,dasd,lsd,lsd1,lsd2,lsd3]
for a in imgobjs:
    for i in a:
        imgobj=path +i
        print(imgobj)
        shibie(time.sleep(31) if i == 'time.png' else imgobj)
        time.sleep(3)